package nsu.edu.zsq.controller.sys;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.Course;
import nsu.edu.zsq.bean.SchMajor;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.exception.ParamException;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.CourseService;
import nsu.edu.zsq.service.sys.SchMajorService;
import nsu.edu.zsq.util.UploadFileUtil;

/** 
* Description: 学校课程的controller类<br>
*/
@RequestMapping("/sys/school/course")
@Controller
public class CourseController {
    
    @Autowired
    private CourseService courseService;
    @Autowired
    private SchMajorService schMajorService;
    
    /**
     * 获取院系+专业的tree结构数据
     * @return JsonData
     */
    @RequestMapping("/schOrgWithMajorTree")
    @ResponseBody
    public JsonData schOrgWithMajorTree() {
        String listSchOrgWithMajorTree = courseService.listSchOrgWithMajorTree();
        List<SchMajor> listSchMajor = schMajorService.listSchMajor();
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("listSchOrgWithMajorTree", listSchOrgWithMajorTree);
        map.put("listSchMajor", listSchMajor);
        return JsonData.success(map);
    }

    /**
     * 通过专业代码获取带有分页信息的课程列表
     * @param majorNo 专业代码
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listCourse")
    @ResponseBody
    public JsonData listCourse(@RequestParam("majorNo") String majorNo, PageQuery pageQuery) {
        PageInfo<Course> listCourseWithPage = courseService.listCourseWithPage(majorNo, pageQuery);
        return JsonData.success(listCourseWithPage);
    }
    
    
    /**
     * 单个或者批量删除课程
     * @param courseId 课程id数组
     * @return JsonData
     */
    @RequestMapping("/delCourse")
    @ResponseBody
    public JsonData delCourse(@RequestParam("courseId[]")int courseId[]) {
        if (courseId.length > 1) {
            // 批量删除
            courseService.deleteBatchCourse(courseId);
        } else if (courseId.length == 1) {
            // 单个删除
            courseService.deleteCourse(courseId[0]);
        } else {
            throw new ParamException("courseId无参数");
        }
        return JsonData.success();
    }
    
    /**
     * 携带课程所在的专业代码，跳转到添加课程信息的页面
     * @param majorNo 专业代码
     * @param model 
     * @return String manager/school/course_add.html
     */
    @RequestMapping("/toAddCourse")
    public String toAddCourse(@RequestParam("majorNo") String majorNo, Model model) {
        model.addAttribute("majorNo", majorNo);
        return "manager/school/course_add.html";
    }
    
    /**
     * 跳转到编辑课程页面
     * @param courseId 课程id
     * @param model 
     * @return String manager/school/course_edit.html
     */
    @RequestMapping("/toEditCourse")
    public String toEditCourse(@RequestParam("courseId") int courseId, Model model) {
        Course course = courseService.getCourse(courseId);
        model.addAttribute("course", course);
        return "manager/school/course_edit.html";
    }
    
    /**
     * 添加一条课程信息
     * @param course 课程信息
     * @return JsonData
     */
    @RequestMapping("/saveCourse")
    @ResponseBody
    public JsonData saveCourse(Course course) {
        courseService.saveCourse(course);
        return JsonData.success();
    }
    
    /**
     * 更新一条课程信息
     * @param course 课程信息
     * @return JsonData
     */
    @RequestMapping("/editCourse")
    @ResponseBody
    public JsonData editCourse(Course course) {
        courseService.updateCourse(course);
        return JsonData.success();
    }
    
    
    
    /**
     * 获取选课的分页数据
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listPublicCourse")
    @ResponseBody
    public JsonData listPublicCourse(PageQuery pageQuery) {
        PageInfo<Course> listPublicCourse = courseService.listPublicWithPage(pageQuery);
        return JsonData.success(listPublicCourse);
    }
    
    
    /**
     * 跳转到编辑公共选修课程页面
     * @param courseId 课程id
     * @param model 
     * @return String manager/school/publicCourse_edit.html
     */
    @RequestMapping("/toEditPublicCourse")
    public String toEditPublicCourse(@RequestParam("courseId") int courseId, Model model) {
        Course course = courseService.getCourse(courseId);
        model.addAttribute("course", course);
        return "manager/school/publicCourse_edit.html";
    }
    
}
